<template>
  <el-form inline="true">
    <el-form-item>
      <selector v-model="params.difficulty" placeholder="请选择题目难度" style="width: 200px;"></selector>
    </el-form-item>
    <el-form-item>
      <el-input v-model="params.title" placeholder="请您输入要搜索的题目标题" />
    </el-form-item>
    <el-form-item>
      <el-button plain @click="onSearch">搜索</el-button>
      <el-button plain type="info" @click="onReset">重置</el-button>
      <el-button plain type="primary" :icon="Plus" @click="onAddQuestion">添加题目</el-button>
    </el-form-item>
  </el-form>
  <el-table height="526px" :data="questionList">
    <el-table-column prop="questionId" width="180px" label="题目id" />
    <el-table-column prop="title" label="题目标题" />
    <el-table-column prop="difficulty" label="题目难度" width="90px">
      <template #default="{ row }">
        <div v-if="row.difficulty === 1" style="color:#3EC8FF;">简单</div>
        <div v-if="row.difficulty === 2" style="color:#FE7909;">中等</div>
        <div v-if="row.difficulty === 3" style="color:#FD4C40;">困难</div>
      </template>
    </el-table-column>
    <el-table-column prop="createName" label="创建人" width="140px" />
    <el-table-column prop="createTime" label="创建时间" width="180px" />
    <el-table-column label="操作" width="100px" fixed="right">
      <template #default="{ row }">
        <el-button type="text" @click="onEdit(row.questionId)">编辑</el-button>
        <el-button type="text" class="red" @click="onDelete(row.questionId)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

  <el-pagination size="small" background layout="total, sizes, prev, pager, next, jumper" :total="total"
    v-model:current-page="params.pageNum" v-model:page-size="pageSize" :page-sizes="[1, 5, 10, 15, 20]"
    @size-change="handleSizeChange" @current-change="handleCurrentChange" />

  <question-drawer ref="questionDrawerRef" @success="onSuccess"></question-drawer>

</template>

<script setup>
import { Plus } from "@element-plus/icons-vue"
import { ref } from 'vue';
import Selector from "@/components/QuestionSelector.vue"
import { reactive } from 'vue'
import { delQuestionService, getQuestionListService } from "@/apis/question";
import QuestionDrawer from "@/components/QuestionDrawer.vue";

const params = reactive({
  pageNum: 1,
  pageSize: 10,
  difficulty: '',
  title: ''
});

const total = ref(0);
const questionList = ref([])
async function getQuestionList() {
  const result = await getQuestionListService(params);
  questionList.value = result.rows;
  total.value = result.total;
}

getQuestionList();

function handleSizeChange(newSize) {
  params.pageSize = newSize;
  params.pageNum = 1;
  getQuestionList();
}

function handleCurrentChange(newPage) {
  getQuestionList();
}

function onSearch() {
  params.pageNum = 1;
  getQuestionList();
}

function onReset() {
  params.pageNum = 1;
  params.pageSize = 10;
  params.title = '';
  params.difficulty = '';
  getQuestionList();
}

const questionDrawerRef = ref()

function onAddQuestion() {
  questionDrawerRef.value.open();
}

function onSuccess(service) {
  if (service == 'add') {
    params.pageNum = 1;
  }
  getQuestionList();
}

async function onEdit(questionId) {
  questionDrawerRef.value.open(questionId);
  // await getQuestionDetailService(questionId);
}

// 点击删除按钮时先显示确认对话框
async function onDelete(questionId) {
  // 弹出确认框询问用户是否确定删除
  const confirmResult = await ElMessageBox.confirm('确定要删除该题目吗？', '删除确认', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
  });
  if (confirmResult === 'confirm') {
    // 用户确认，执行删除等后续操作
    await delQuestionService(questionId);
    // 根据实际需求重置参数，这里假设重置页码
    const params = {
      pageNum: 1,
    };
    await getQuestionList(params);
  }
}


// const questionList = reactive([
//   {
//     questionId: '1',
//     title: '题目1',
//     difficulty: 1,
//     createName: '超级管理员',
//     createTime: '2024-05-30 17:00:00'
//   },
//   {
//     questionId: '2',
//     title: '题目2',
//     difficulty: 2,
//     createName: '超级管理员',
//     createTime: '2024-05-30 17:00:00'
//   },
//   {
//     questionId: '3',
//     title: '题目3',
//     difficulty: 3,
//     createName: '超级管理员',
//     createTime: '2024-05-30 17:00:00'
//   }
// ])
</script>